package com.hit.server.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hit.server.api.entity.RobotGroupEntity;
import com.hit.server.api.dto.RobotGroupQueryDTO;
import com.hit.server.api.dto.RobotGroupSaveDTO;
import com.hit.server.api.dto.RobotGroupUpdateDTO;
import com.hit.server.api.entity.SysCustomerEntity;
import com.hit.server.api.entity.SysProjectEntity;
import com.hit.server.api.vo.RobotGroupVO;
import com.hit.server.biz.mapper.RobotGroupMapper;
import com.hit.server.api.service.RobotGroupService;
import com.hit.server.biz.mapper.SysProjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

/**
 * 机器人分组
 *
 * @author kehanjiang
 * @date 2025-04-03 17:25:13
 */
@Service
@RequiredArgsConstructor
public class RobotGroupServiceImpl extends ServiceImpl<RobotGroupMapper, RobotGroupEntity> implements RobotGroupService {

    private final SysProjectMapper sysProjectMapper;

    private RobotGroupVO convertToVO(Object o) {
        if (o instanceof RobotGroupEntity) {
            RobotGroupVO vo = new RobotGroupVO();
            BeanUtil.copyProperties(o, vo);
            //设置其他参数
            Optional.ofNullable(vo.getProjectId()).ifPresent(projectId -> {
                SysProjectEntity project = sysProjectMapper.selectById(projectId);
                if (project != null) {
                    vo.setProjectName(project.getName());
                }
            });
            return vo;
        }
        return null;
    }

    private void preCheck(RobotGroupEntity entity, Boolean update) {
        Long id = entity.getId();
        //String name = entity.getName();
        if (update) {//更新操作
            Assert.notNull(id, "id不可为空！");
        } else {
            //Assert.hasText(name, "name不可为空！");
        }
    }

    @Override
    public Page<RobotGroupVO> getRobotGroupPage(Page page, RobotGroupQueryDTO dto) {
        RobotGroupEntity entity = new RobotGroupEntity();
        BeanUtils.copyProperties(dto, entity);
        entity.setName(null);
        final String name = dto.getName();
        LambdaQueryWrapper<RobotGroupEntity> wrapper = new LambdaQueryWrapper<>(entity);
        wrapper.like(StringUtils.hasText(name), RobotGroupEntity::getName, name);
        wrapper.orderByAsc(RobotGroupEntity::getId);
        return (Page<RobotGroupVO>) this.page(page, wrapper)
                .convert(this::convertToVO);
    }

    @Override
    public RobotGroupVO getRobotGroupById(Long id) {
        Assert.notNull(id, "id不可为空！");
        return convertToVO(this.getById(id));
    }


    @Override
    public Boolean saveRobotGroup(RobotGroupSaveDTO dto) {
        RobotGroupEntity entity = new RobotGroupEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, false);
        return this.save(entity);
    }

    @Override
    public Boolean updateRobotGroupById(RobotGroupUpdateDTO dto) {
        RobotGroupEntity entity = new RobotGroupEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, true);
        return this.updateById(entity);
    }

    @Override
    public Boolean removeRobotGroupBatchByIds(List<Long> ids) {
        Assert.notEmpty(ids, "ID不可为空！");
        return this.removeBatchByIds(ids);
    }
}